package com.cai.dao.impl;

import com.cai.dao.UserDao;
import com.cai.entity.User;
import com.cai.util.C3p0Util;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author RuoLi
 * 实现类
 */
public class UserDaoImpl implements UserDao {
    static QueryRunner runner = new QueryRunner(C3p0Util.getDataSource());
    List<User> userList = new ArrayList<>();

    @Override
    public int userLogin(User user) {
        String sql = "select * from user where user=? and password=? ";
        try {
            Object[] objects = {user.getUser(), user.getPassword()};
            user = runner.query(sql, new BeanHandler<>(User.class), objects);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (user.getUser_id() != 0) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public User userInfoLogin(User user) {
        String sql = "select * from user where user=? and password=? ";
        try {
            Object[] objects = {user.getUser(), user.getPassword()};
            user = runner.query(sql, new BeanHandler<>(User.class), objects);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (user.getUser_id() != 0) {
            return user;
        } else {
            return null;
        }
    }

    @Override
    public int userUpdatePassword(String newPassword, User user) {
        String sql = "UPDATE user SET password = ? WHERE user=? and password=?";
        int num = 0;
        try {
            Object[] objects = {newPassword, user.getUser(), user.getPassword()};
            num = runner.update(sql, objects);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return num;
    }

    @Override
    public int userInsert(User user) {
        String sql = "INSERT INTO user(user_name,user_type,user_email,user_phone,user,password) VALUES (?, ?, ?, ?, ?, ?)";
        int num = 0;
        try {
            Object[] objects = {user.getUser_name(), user.getUser_type(), user.getUser_email(), user.getUser_phone(), user.getUser(), user.getPassword()};
            num = runner.update(sql, objects);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return num;
    }

    @Override
    public List<User> userQueryAll() {
        String sql = "select * from user";
        try {
            userList = runner.query(sql, new BeanListHandler<>(User.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userList;
    }

    @Override
    public int userDelete(String user) {
        String sql = "DELETE FROM user WHERE user =?";
        try {
            int count = runner.update(sql, new Object[]{user});
            if (count != 0) {
                return 1;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    @Override
    public int userUpdate(User user) {
        String sql = "UPDATE user SET user_name = ?, user_type = ?, user_email = ?, user_phone = ? WHERE  user = ?";
        Object[] params = {user.getUser_name(),user.getUser_type(),user.getUser_email(),user.getUser_phone(),user.getUser()};
        try {
            int count = runner.update(sql, params);
            if (count != 0) {
                return 1;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    @Override
    public int userIsNull(String name) {
        String sql = "select count(*) from user where user_name=?";
        return UtilsDaoImpl.selectCount(sql,new String[]{name});
    }
}
